package com.snl.micro.blog.article.service.impl;

import com.snl.micro.blog.article.base.ScheThread;
import com.snl.micro.blog.article.client.UserClient;
import com.snl.micro.blog.article.dao.mapper.ArticleMapper;
import com.snl.micro.blog.article.dao.repository.ArticleRepository;
import com.snl.micro.blog.article.pojo.po.ArticlePo;
import com.snl.micro.blog.article.service.ArticleService;
import com.snl.micro.blog.common.action.Action;
import com.snl.micro.blog.common.action.UserAction;
import com.snl.micro.blog.common.base.pager.Pager;
import com.snl.micro.blog.common.base.schuleThreadPool.CommonPool;
import com.snl.micro.blog.common.param.ArticleSaveParam;
import com.snl.micro.blog.common.protocol.ResponseResult;
import com.snl.micro.blog.common.query.ArticleQueryParam;
import com.snl.micro.blog.common.query.CommentQuery;
import com.snl.micro.blog.common.query.TagQuery;
import com.snl.micro.blog.common.query.UserQuery;
import com.snl.micro.blog.common.vo.ArticleVo;
import com.snl.micro.blog.common.vo.CommentVo;
import com.snl.micro.blog.common.vo.TagVo;
import com.snl.micro.blog.common.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.ExecutionException;

@Slf4j
@Service
public class ArticleServiceImplement implements ArticleService {

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private ScheThread scheThread;

    @Autowired
    private UserClient userClient;

    @Override
    public Pager<List<ArticleVo>> query(ArticleQueryParam queryParam) {
        log.debug("入参:{}",queryParam);
        TagQuery tagQuery = new TagQuery();
        Pager<List<ArticleVo>> pager = new Pager<>();
        List<ArticleVo> results = new ArrayList<>();
        UserQuery userQuery = new UserQuery();
        CommentQuery commentQuery = new CommentQuery();
        //如果当前查询以用户id为例，那么就是具体的以用户参训
        //此刻total参数应该是当前用户的所有文章？
        //不对逻辑有缺陷
        Long total = articleRepository.selectAllArticles(queryParam);
        List<ArticlePo> query = articleRepository.query(queryParam);
        query.forEach(p->{
            ArticleVo articleVo = new ArticleVo();
            Long id = p.getId(); //文章id
            Long userId = p.getUserId(); //作者id
            HashMap<Action, Integer> actionIntegerHashMap =
                    this.scheThread.countLikesAndHates(id);
            userQuery.setId(userId);
            commentQuery.setArticleId(id);
            List<UserVo> userVo = scheThread.getUserVo(userQuery);
            List<CommentVo> comments = scheThread.getComments(commentQuery);
            tagQuery.setArticleId(id);
            List<TagVo> tags = scheThread.runTag(tagQuery);
            BeanUtils.copyProperties(p,articleVo);
            articleVo.setTags(tags);
            articleVo.setLikes(actionIntegerHashMap.get(Action.LIKE));
            articleVo.setHates(actionIntegerHashMap.get(Action.HATE));
            articleVo.setUserVo(userVo.get(0));
            articleVo.setComments(comments);
            results.add(articleVo);
        });
        pager.setTotal(total);
        pager.setData(results);
        return pager;
    }

    @Override
    public void save(ArticleSaveParam param) {
        log.debug("业务层:{}",param);
        ArticlePo articlePo = new ArticlePo();
        BeanUtils.copyProperties(param,articlePo);
        //首先先插入文章，并获取id
        articlePo.setCreateTime(LocalDateTime.now());
        articlePo.setUpdateTime(LocalDateTime.now());
        articleRepository.insertArticle(articlePo);

        //获取文章id，并获取标签
        Long id = articlePo.getId();//文章id
        List<Integer> tags = param.getTags();
        this.scheThread.insertArticleTag(id,tags);
        UserAction userAction = new UserAction();
        userAction.setCreateTime(LocalDateTime.now());
        userAction.setUsername("xxx");
        userAction.setMessage("发布了新文章");
        CommonPool.addAction(userAction);
    }
}
